package com.weedsdream

import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.WindowManager
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.unit.IntOffset
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.weedsdream.BaseActivity.Companion.activity
import com.weedsdream.BaseActivity.Companion.screenSize
import kotlin.reflect.KClass


/**项目根Activity
 * @property activity 当前系统正在显示的activity
 * @property screenSize 当前设备可用的屏幕尺寸-包含边衬区*/
open class BaseActivity : AppCompatActivity() {
    companion object {
        lateinit var activity: BaseActivity
        var screenSize = IntOffset.Zero
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        hideStatusBar()
        activity = this
    }

    /**隐藏状态栏*/
    private fun hideStatusBar() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.hide(WindowInsetsCompat.Type.statusBars())//隐藏状态栏
        controller.hide(WindowInsetsCompat.Type.navigationBars())//隐藏导航栏
        controller.systemBarsBehavior =//显示后自动隐藏
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        enableEdgeToEdge()//取消刘海屏状态栏边衬区占用

        // 获取实际的屏幕尺寸
        val metrics = DisplayMetrics()
        (getSystemService(WINDOW_SERVICE) as WindowManager).defaultDisplay.getRealMetrics(metrics)
        screenSize = IntOffset(metrics.widthPixels, metrics.heightPixels)
    }

    /**简单开启一个新的界面*/
    fun startActivity(cls: KClass<*>) {
        startActivity(Intent(this, cls.java))
    }

    /**获取app名*/
    fun getAppName(): String {
        return applicationContext.packageManager.getApplicationLabel(
            applicationContext.packageManager.getApplicationInfo(
                applicationContext.packageName, 0
            )
        ).toString()
    }
}